<template>
  <div class="list">
    <div class="content-head" style="margin-bottom: 20px;">
      <div class="sort-keys">
        <a-input placeholder="商品标题" style="width: 160px;margin-right:10px;" v-model="key" allowClear :size="size"
          @input="onSearch" />
        <span style="margin-left: 10px; margin-right: 6px;">日期:</span>
        <iDatePicker :showTime="false" style="margin-left: 2px;" placeholder="不选则不限制" v-model="filter.min_day"></iDatePicker>
        至
        <iDatePicker :showTime="false" style="margin-right: 6px;" placeholder="不选则不限制" v-model="filter.max_day"></iDatePicker>
        <a-select style="width: 150px;margin-right:10px;" v-model="admin_id" placeholder="采购员" allowClear v-if="action_show">
          <a-select-option :value="item.id" v-for="item in admin_list" :key="item.id">{{ item.title }}</a-select-option>
        </a-select>
        <a-checkbox :checked="isOutPriceCheck ? true : false" @change="onIsOutPriceCheck" :size="size">是否只看溢价</a-checkbox>
        
        <a-button :size="size" type="primary" @click="refreshData" style="margin-right:10px;">刷新</a-button>
      </div>

    </div>
    
    <div>
      <span>
        采购总量:  <span class="value-font"> {{total_num}} </span>
      </span>
      <span style="margin-left: 20px;">
        采购总额:  <span class="value-font"> {{total_money_price}} </span>
      </span>
      <span style="margin-left: 20px;">
        商城总额:  <span class="value-font"> {{product_total_price}} </span>
      </span>      
    </div>
    
    <a-table class="table" :rowKey="row => row.uuid" :dataSource="list" :pagination="pagination" :size="size">
      <a-table-column title="商品名称" width="120px">
        <template slot-scope="row">
          <div>{{row.title}}</div>
        </template>
      </a-table-column>
      <a-table-column title="商城价格" width="120px">
        <template slot-scope="row">
          <div>{{row.product_price}}</div>
        </template>
      </a-table-column>
      
      <a-table-column title="采购单价" width="120px">
        <template slot-scope="row">
          <div>{{(row.money_price / row.num).toFixed(2)}}</div>
        </template>
      </a-table-column>

      <a-table-column title="采购数量" width="30px">
        <template slot-scope="row">
          <div>{{row.num}}个</div>
        </template>
      </a-table-column>

      <a-table-column title="采购总价" width="80px">
        <template slot-scope="row">
          <div :style="{ color: parseFloat(row.money_price) > parseFloat(row.product_total_price) ? 'red' : 'inherit' }">{{row.money_price}}</div>
        </template>
      </a-table-column>
      
      <a-table-column title="溢价金额" width="80px">
        <template slot-scope="row">
          <div v-if="parseFloat(row.money_price) > parseFloat(row.product_total_price)" style="color:red">{{(row.money_price - row.product_total_price).toFixed(2)}}</div>
        </template>
      </a-table-column>
      
    </a-table>
    <ExportPopup2 @hide="isExport2=false" :isShow="isExport2"></ExportPopup2>
  </div>
</template>
<script>
  import moment from 'moment'
  import ISwitch from '@/components/ISwitch/ISwitch'
  import ExportPopup2 from "./components/ExportPopup2"
  import IDatePicker from "@/components/IDatePicker/IDatePicker.vue"
  const resourceType = 'storage_buy_tongji'

  export default {
    components: {
      ISwitch,
      ExportPopup2,
      IDatePicker
    },
    props: {
      selectMode: {
        type: Boolean
      },
      params: {
        type: Object
      },
      size: {
        type: String,
        default: 'default'
      }
    },
    data() {
      return {
        isOutPriceCheck: false,
        action_show: false,
        admin_list: [],
        admin_id: undefined,
        isExport2: false,
        isEdit: false,
        editUuid: '',
        list: [],
        page: 1,
        per_page: 10,
        total: 0,
        total_num: 0,
        total_money_price: 0,
        product_total_price: 0,
        key: '',
        status: '1',
        filter: {
          min_day: '',
          max_day: ''
        },        
        frame_add: [
          {
            title: '标题',
            key: 'title',
            newline: true,
            required: true,
          },
          {
            title: '数量',
            key: 'num',
            type: 'number',
            newline: true,
            required: true,
          },
          {
            title: '货物总价',
            key: 'money_price',
            type: 'number',
            newline: true,
            required: true,
          },
          {
            title: '货运公司',
            key: 'express_name',
            newline: true,
          },
          {
            title: '货运单号',
            key: 'express_number',
            newline: true,
          },
          {
            title: '备注',
            type: 'textarea',
            key: 'remark',
            span: 22,
          },
          {
            title: '付款附件',
            type: 'image',
            key: 'image',
            showLibrary: false,
            required: true,
            span: 22,
          },
          {
            title: '签收附件',
            type: 'image',
            key: 'image2',
            showLibrary: false,
            span: 22,
            where: {
              status: 2
            },
            required: true,
          },
          {
            title: '退货附件',
            type: 'image',
            key: 'tuihuo_image',
            showLibrary: false,
            span: 22,
            where: {
              status: 4
            },
            required: true,
          },          
          {
            title: '状态',
            key: 'status',
            type: 'radio',
            default: 1,
            required: true,
            span: 24,
            options: [          
              {
                title: '物流中',
                value: 1,
              }
            ],
          },
          ],
          frame_edit: [
          {
            title: '标题',
            key: 'title',
            newline: true,
            required: true,
            disabled: true
          },
          {
            title: '数量',
            key: 'num',
            type: 'number',
            newline: true,
            required: true,
          },
          {
            title: '货物总价',
            key: 'money_price',
            type: 'number',
            newline: true,
            required: true,
          },
          {
            title: '货运公司',
            key: 'express_name',
            newline: true,
          },
          {
            title: '货运单号',
            key: 'express_number',
            newline: true,
          },
          {
            title: '备注',
            type: 'textarea',
            key: 'remark',
            span: 22,
          },
          {
            title: '付款附件',
            type: 'image',
            key: 'image',
            showLibrary: false,
            required: true,
            span: 22,
          },
          {
            title: '签收附件',
            type: 'image',
            key: 'image2',
            showLibrary: false,
            span: 22,
            where: {
              status: 2
            },
            required: true,
          },
          {
            title: '退货附件',
            type: 'image',
            key: 'tuihuo_image',
            showLibrary: false,
            span: 22,
            where: {
              status: 4
            },
            required: true,
          },          
          {
            title: '状态',
            key: 'status',
            type: 'radio',
            default: 1,
            required: true,
            span: 24,
            options: [
              {
                title: '已取消',
                value: 3,
              },            
              {
                title: '物流中',
                value: 1,
              },
              {
                title: '已入库',
                value: 2,
              },
              {
                title: '已退货',
                value: 4,
              }              
            ],
          },
          ],          
        statusList: [
          {
            text: '物流中',
            key: '1'
          },
          {
            text: '已入库',
            key: '2'
          },
          {
            text: '已取消',
            key: '3'
          },
          {
            text: '已退货',
            key: '4'
          },          
          {
            text: '全部',
            key: 'all'
          },
        ],
      }
    },
    watch: {
      params() {
        this.refreshData()
      },
      admin_id() {
        this.refreshData()
      },
      status() {
        this.refreshData()
      },
      'filter.min_day'() {
        this.refreshData()
      },
      'filter.max_day'() {
        this.refreshData()
      }      
    },
    computed: {
      pagination() {
        return {
          defaultPageSize: this.per_page,
          defaultCurrent: this.page,
          total: this.total,
          onChange: (e) => {
            this.page = e
            this.fetchData()
          }
        }
      }
    },
    filters: {
      min_day: '',
      max_day: ''
    },
    created() {
      this.action_show = this.$authCheck('storage_buylist')
      this.$http.get('/storage_admin_list').then(res => {
        this.admin_list = res.data.list
        this.admin_list = this.admin_list.map(item => {
          return {
            title: item.name,
            id: item.admin_id
          }
        })
      })
      this.fetchData()
    },
    methods: {
      handleDelete(row) {
        this.$confirm({
          title: '提示',
          content: `删除会影响现有库存,确认删除吗？`,
          onOk: () => {
            this.$http.delete(`/${resourceType}/${row.uuid}`).then(res => {
              this.fetchData()
            })
          }
        })
      },
      onIsOutPriceCheck (e) {
        this.isOutPriceCheck = e.target.checked
        this.refreshData()
      },
      handleCreate() {
        this.$iForm.show({
          title: '新增',
          frame: this.frame_add,
          success: (data) => {
            this.$http.post(`/storage_buy`, {
              ...data,
            }).then(res => {
              this.fetchData()
              this.$iForm.hide()
            })
          },
        })
      },
      handleEdit(row) {
        this.$iForm.show({
          title: '编辑',
          frame: this.frame_edit,
          form: row,
          success: (data) => {
            this.$http.put(`/storage_buy/${row.uuid}`, {
              type: 'update',
              attributes: {
                ...data
              }
            }).then(res => {
              this.fetchData()
              this.$iForm.hide()
            })
          },
        })
      },
      refreshData() {
        this.page = 1
        this.key = ''
        this.fetchData()
      },
      onSearch(e) {
        this.fetchData()
      },
      fetchData() {
        this.isEdit = false
        this.$http.get(`/${resourceType}`, {
          params: {
            ...this.params,
            page: this.page,
            per_page: this.per_page,
            key: this.key,
            status: this.status,
            admin_id: this.admin_id,
            is_out_price_check: this.isOutPriceCheck,
            created_start_at: this.filter.min_day,
            created_end_at: this.filter.max_day
          }
        }).then(res => {
          this.list = res.data.list
          this.total = res.data.item_total
          this.total_num = res.data.total_num
          this.total_money_price  = res.data.total_money_price
          this.product_total_price = res.data.product_total_price
        }).finally(() => {

        })
      },
    }
  }
</script>
<style lang="scss" scoped>
  .item-key {
    font-size: 86%;
    color: gray;
  }
  .value-font {
    font-weight: 500;
    font-size: 110%;
  }
  .account-qrcode {
    width: 40px;
    cursor: pointer;
  }
  .sort-keys {
    display: flex;
    align-items: center;

    .tab-list {
      display: flex;
      margin-top: 6px;
      border-bottom: 1px solid #e1e1e1;
      margin-right: 20px;

      .item {
        margin-right: 30px;
        padding: 0px 10px 4px 10px;
        cursor: pointer;
        font-weight: 500;

        &.actived {
          color: #1890FF;
          border-bottom: 3px solid #1890FF;
        }

        &:hover {
          color: #1890FF;
        }
      }
    }

  }

  .top-btn {
    background: #1890FF;
    color: white;
    cursor: pointer;
    font-size: 80%;
    border-radius: 4px;
    padding: 2px 6px;

    &.unset {
      border: 1px solid #D8D5D5;
      background: #D8D5D5;
      color: gray;
    }
  }

  .content-head {
    display: flex;
    justify-content: space-between;
    flex-wrap: wrap;
  }

  .table {
    .thumb {
      height: 60px;
      border-radius: 6px;
    }

    .content-image {
      height: 60px;
      width: 60px;
      margin-right: 10px;
      margin-bottom: 10px;
      object-fit: cover;
      border-radius: 10px;
      cursor: pointer;

      &:hover {
        opacity: 0.8;
      }
    }

    .user {
      display: flex;
      // flex-wrap: wrap;
      align-items: center;

      img {
        width: 26px;
        height: 26px;
        margin-right: 6px;
        border-radius: 50%;
        overflow: hidden;
      }

      .gender-man,
      .gender-woman {
        margin-left: 5px;
        font-size: 12px;
      }

      .gender-man {
        color: #409EFF;
      }

      .gender-woman {
        color: #F56C6C;
      }
    }
  }
</style>
